package com.hhm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hhm.mapper.MainsMapper;
import com.hhm.pojo.*;
import com.hhm.service.MainsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务实现类
 */
@Service
@Transactional
public class MainsServiceImpl implements MainsService {
    @Autowired
    private MainsMapper mm;

    /**
     * 分页
     * @param mains
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public PageInfo<Mains> findAllByPage(Mains mains, Integer pageNum, Integer pageSize) {
        //开启分页
        PageHelper.startPage(pageNum, pageSize);
        //获取数据
        List<Mains> list = mm.findAll(mains);

        return new PageInfo<>(list);
    }

    /**
     * 添加主表--再添加从表
     * @param mains
     * @return
     */
    @Override
    public int add(Mains mains) {
        //添加主表
        int i = mm.add(mains);
        //添加从表
        List<Infos> infos = mains.getInfos();
        if (infos!=null && infos.size()>0){
            mm.addInfo(mains.getId(), infos);
        }

        return i;
    }

    /**
     * 查询地区
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public List<Area> areas() {
        return mm.areas();
    }

    /**
     * 查询乡镇
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public List<Country> countrys() {
        return mm.countrys();
    }

    /**
     * 查询小区
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public List<Garden> gardens() {
        return mm.gardens();
    }

    /**
     * 删除主表---删除从表
     * @param ids
     * @return
     */
    @Override
    public int del(Integer[] ids) {
        mm.delInfo(ids);

        return mm.del(ids);
    }

    /**
     * 存储过程--统计
     * @return
     */
    @Override
    public List<Mains> count() {
        return mm.count();
    }
}
